import random


def merge(li, low, mid, high):
    i = low
    j = mid + 1
    ltmp = []
    while i <= mid and j <= high:
        if li[i] < li[j]:
            ltmp.append(li[i])
            i += 1
        else:
            ltmp.append(li[j])
            j += 1
    while i <= mid:
        ltmp.append(li[i])
        i += 1
    while j <= high:
        ltmp.append(li[j])
        j += 1
    li[low:high + 1] = ltmp
    return li


# li = [2,4,5,7,1,3,6,8]
# print(li)
# li = merge(li,0,3,7)
# print(li)
def merge_sort(li: list, low: int, high: int):
    if low < high:  # 至少有两个元素
        mid = (low + high) // 2
        merge_sort(li, low, mid)
        merge_sort(li, mid + 1, high)
        merge(li, low, mid, high)
    return li


li = [random.randint(1, 100) for i in range(20)]
print(li)
li = merge_sort(li, 0, len(li) - 1)
print(li)
